package edu.ubb.remeye.logging;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

/**
 * The logger class, it's using log4j. It is implemented using
 * the Singleton design pattern.
 * 
 * @author Andrew Ianicicau
 * 
 */
public class RemEyeLogger {

	///////////////////////////////////////////////////////////////////////////////////
	////		ATTRIBUTES - RemEyeLogger
	///////////////////////////////////////////////////////////////////////////////////

	/**
	 * Logger object.
	 */
	private Logger log;
	
	/**
	 * Object of this class for Singleton pattern.
	 */
	private static RemEyeLogger instance;
	
	/**
	 * Logger properties file path.
	 */
	public static final String LOGGER_PROPERTIES_FILENAME = "properties/log4j.properties";

	
	
	///////////////////////////////////////////////////////////////////////////////////
	////		CONSTRUCTORS - RemEyeLogger
	///////////////////////////////////////////////////////////////////////////////////

	private RemEyeLogger() {
		log = Logger.getLogger(RemEyeLogger.class);
		PropertyConfigurator.configure(LOGGER_PROPERTIES_FILENAME);
	}
	
	
	
	///////////////////////////////////////////////////////////////////////////////////
	////		METHODS - RemEyeLogger
	///////////////////////////////////////////////////////////////////////////////////
	
	/**
	 * @return Logger instance.
	 */
	public static RemEyeLogger getInstance() {
		if (instance == null) {
			instance = new RemEyeLogger();
		}
		return instance;
	}

	/**
	 * Get the logger.
	 * @return Log4j logger object.
	 */
	public Logger getLogger() {
		return this.log;
	}
	
	/**
	 * Gets the object's class name.
	 * @param obj The object.
	 * @return The object's class name.
	 */
	private static String getClassName(Object obj) {
		if (obj == null) {
			return "NULL";
		}
		return obj.getClass().getSimpleName();
	}
	
	/**
	 * Returns the desired log formatted message from the object and message.
	 * @param obj The object.
	 * @param message The message.
	 * @return Log formatted message.
	 */
	private static String getFormatedMessage(Object obj, Object message) {
		String sourceName = (obj == null) ? "" : "[" + getClassName(obj).trim() + "]";
		return sourceName + " " + message;
	}
	
	public static void trace(Object source, Object message) {
	    RemEyeLogger.getInstance().log.trace(getFormatedMessage(source, message));
	  }

	public static void debug(Object source, Object message) {
		RemEyeLogger.getInstance().log.debug(getFormatedMessage(source, message));
	  }
	
	public static void info(Object source, Object message) {
		RemEyeLogger.getInstance().log.info(getFormatedMessage(source, message));
	  }
	
	public static void warn(Object source, Object message) {
		RemEyeLogger.getInstance().log.warn(getFormatedMessage(source, message));
	  }
	
	public static void warn(Object source, Object message, Exception e) {
		RemEyeLogger.getInstance().log.warn(getFormatedMessage(source, message), e);
	  }
	
	public static void error(Object source, Object message) {
		RemEyeLogger.getInstance().log.error(getFormatedMessage(source, message));
	  }
	
	public static void error(Object source, Object message, Exception e) {
		RemEyeLogger.getInstance().log.error(getFormatedMessage(source, message), e);
	  }
	
	public static void fatal(Object source, Object message) {
		RemEyeLogger.getInstance().log.fatal(getFormatedMessage(source, message));
	  }
	
	public static void fatal(Object source, Object message, Exception e) {
		RemEyeLogger.getInstance().log.fatal(getFormatedMessage(source, message), e);
	  }
	
}
